<?php
namespace app\common\service;

use app\wechat\service\WechatService;
use think\facade\Log;

class WechatTemplate{

    /**
     * 登录成功发送通知
     */
    public static function sendLoginSuccessTemp($user){
        $openid = $user['openid'];
        $template_id = 'uMeKP3PXu0qqLyIV8nmrg-38WhwSyNDCP2s1BwnFkgc';

        $data = [
            'first' => [
                'value' => '欢迎登录知鸟AI，您已登录成功'
            ],
            'keyword1' => [
                'value' => $user['username']
            ],
            'keyword2' => [
                'value' => date('Y-m-d H:i:s', time())
            ],
            'remark' => [
                'value' => '感谢使用，请注意账号安全。'
            ],
        ];

        
        return self::send($openid,$template_id, $data);
    }


    // 订单完成消息提醒
    public static function sendOrderCompleteTemp($user, $document){
        $openid = $user['openid'];
        $template_id = 'CZkTSLJ6zD9dEPKCmSsnT4Qf8eE8K6LlKDIupb3MPJk';

        $data = [
            'first' => [
                'value' => '您的论文任务已完成。'
            ],
            'keyword1' => [
                'value' => $document['title']
            ],
            'keyword2' => [
                'value' => date('Y-m-d H:i:s', time())
            ],
            'remark' => [
                'value' => '感谢使用。'
            ],
        ];
        return self::send($openid,$template_id, $data);
    }

    // 发送模板消息 
    public static function send(string $openid, string $template_id, array $data, string $url = ''){
        $result = false;
        if($openid && $template_id && $data){
            try {
                $template = WechatService::WeChatTemplate();
                $send_data = [
                    'touser' => $openid,
                    'template_id' => $template_id,
                    'data' => $data,
                ];
                if($url) $send_data['url'] = $url;
                $result = $template->send($send_data);
            } catch (\Exception $e) {
                Log::notice($e->getMessage());
            }
        }

        return $result;
    }

    // 储值成功后的微信模板消息通知
    public static function sendRechargeSuccessTemp($user, $source, $money, $balance){
        $openid = $user['openid'];
        $template_id = '4AjUyGBcqQAFVmGsxWseaLbCiQ9NO0Vuya3CQ6Za4uQ';

        $data = [
            'first' => [
                'value' => '您已成功充值。当前余额：'.$balance.'元。'
            ],
            'keyword1' => [
                'value' => date('Y-m-d H:i:s', time())
            ],
            'keyword2' => [
                'value' => $source
            ],
            'keyword3' => [
                'value' => $money
            ],
            'remark' => [
                'value' => '感谢使用。'
            ],
        ];
        return self::send($openid,$template_id, $data);
    }


    // 储值成功后的微信模板消息通知
    public static function sendCreateUserTemp($openid, $phone, $url){
        $template_id = 'RwqLZW00W8tVJH2MpgLCB1i_bWp69cyJCVXRzdIouSw';
        $data = [
            'first' => [
                'value' => '恭喜您，您已成功开通账户。'
            ],
            'keyword1' => [
                'value' => $phone
            ],
            'keyword2' => [
                'value' => date('Y-m-d H:i:s', time())
            ],
            'remark' => [
                'value' => '点击查看更多详情，感谢使用。'
            ],
        ];
        return self::send($openid,$template_id, $data, $url);
    }


    // 降重任务完成提醒
    public static function sendRewriteOrderCompleteTemp($user, $title, $url){
        $openid = $user['openid'];
        $template_id = 'CZkTSLJ6zD9dEPKCmSsnT4Qf8eE8K6LlKDIupb3MPJk';

        $data = [
            'keyword1' => [
                'value' => sprintf("改写任务【%s】", $title)
            ],
            'keyword2' => [
                'value' => date('Y-m-d H:i:s', time())
            ]
        ];
        return self::send($openid,$template_id, $data, $url);
    }


}